home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
gnu
/
gawk
/
gawk213b.zoo
/
test
/
calc1.awk
< prev
next >
Wrap
Text File
|
1991-05-22
|
997b
|
30 lines
# calc1 - reverse-Polish calculator, version 1
# input: arithmetic expressions in reverse Polish
# output: values of expressions
{ for (i = 1; i <= NF; i++)
if ($i ~ /^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)$/) {
stack[++top] = $i
} else if ($i == "+" && top > 1) {
stack[top-1] += stack[top]; top--
} else if ($i == "-" && top > 1) {
stack[top-1] -= stack[top]; top--
} else if ($i == "*" && top > 1) {
stack[top-1] *= stack[top]; top--
} else if ($i == "/" && top > 1) {
stack[top-1] /= stack[top]; top--
} else if ($i == "^" && top > 1) {
stack[top-1] ^= stack[top]; top--
} else {
printf("error: cannot evaluate %s\n", $i)
top = 0
next
}
if (top == 1)
printf("\t%.8g\n", stack[top--])
else if (top > 1) {
printf("error: too many operands\n")
top = 0
}
}